package com.loriot.common.maximum_sum_obtained_of_any_permutation_1589;

import java.util.Arrays;
import java.util.Collections;

/**
 * @Title: Main
 * @Package: com.loriot.common.maximum_sum_obtained_of_any_permutation_1589
 * @Description:
 * @Author: zzy boy
 * @Date: 2025/10/30 10:36
 * @Version: 0.0.1-alpha
 */

public class Main {
    class Solution {
        public int maxSumRangeQuery(int[] nums, int[][] requests) {
            double rst = 0;
            int[] differ = new int[nums.length+1];
            for(int[] item:requests){
                int start = item[0];
                int end = item[1];
                differ[start] += 1;
                differ[end+1] -= 1;
            }
            for(int i=1;i<differ.length;i++){
                differ[i]+=differ[i-1];
            }
            Arrays.sort(nums);
            Arrays.sort(differ);
            for(int i=nums.length;i>=1;i--){
                rst+=(long)differ[i]*nums[i-1];
            }
            return (int)(rst%(Math.pow(10,9)+7));
        }
    }
}
